﻿-- https://www.cnblogs.com/xiii/p/12433798.html
local addonName = "ResetNotifier"
local app = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceEvent-3.0", "AceConsole-3.0")

-- 创建或者加载SavedVariables
ResetSv = ResetSv or {
    today = date("%Y_%m_%d", time()),
    num = 0,
    whisper = false
}
local numVal = nil
local setFrame = nil

-- 重置
local reset_failed = '无法重置'
local reset_success = '已被重置'

-- 创建窗体
local board = CreateFrame("Frame", nil, UIParent,"BackdropTemplate")

-- 启用插件时
function app:OnEnable()
    -- self:Print("插件启用成功 ", ResetSv.today, ResetSv.num)
    -- 判断 是否 今天
    if ResetSv.today ~= date("%Y_%m_%d", time()) then
        -- 更新今日的日期
        ResetSv.today = date("%Y_%m_%d", time())
        ResetSv.num = 0
    end

end

-- 首次加载插件时
function app:OnInitialize()
    -- 注册事件 聊天信息系统
    self:RegisterEvent("CHAT_MSG_SYSTEM")
    initUi()
end

-- 检测 聊天信息系统 msg = '暴风城监狱 已被重置'
function app:CHAT_MSG_SYSTEM(event, msg)
    -- 判断 是否 副本重置
    if strfind(msg, reset_success) or strfind(msg, reset_failed) then
        -- 重置 成功
        if strfind(msg, reset_success) then
            ResetSv.num = ResetSv.num + 1
            numVal:SetText("\124cff00ff00"..ResetSv.num.." \124r")

            -- 判断是否开启 私聊
            if ResetSv.whisper then
                SendChatMessage("{rt1}副本 已重置 "..ResetSv.num.." 次，请进入副本！{rt1}" , "WHISPER", nil, UnitName("player"))    
            end
            
            SendChatMessage("{rt1}副本 已重置"..ResetSv.num.."  次，请进入副本！{rt1}" , "PARTY", nil);
            -- 记录 重置次数
        else
            SendChatMessage("{rt1}副本 重置失败！{rt1}" , "WHISPER", nil, UnitName("player"))
        end
    end
end

-- 初始化ui
function initUi()
    board:SetSize(60, 40)

    -- board:SetBackdrop({
    --     bgFile = "Interface/ChatFrame/ChatFrameBackground",
    --     -- edgeFile = "Interface/ChatFrame/ChatFrameBackground",
    --     tile = true,
    --     edgeSize = 1,
    --     tileSize = 5
    -- });

    board:SetPoint("CENTER")
    board:Show()
    board:SetMovable(true)

    -- 创建 标题
    local title = board:CreateFontString(nil, "OVERLAY")
    title:SetFont(GameFontNormal:GetFont(), 16)
    title:SetPoint("TOPLEFT", board, "TOPLEFT", 2, 18 )
    title:Show()
    title:SetText("\124CFFFF0000 今日副本 \124r ")

    -- 保存ui
    numVal = lineUi("次数", ResetSv.num, 1)

    DragUi()

    initSetUi()

    -- 坐标定位
    if ResetSv.pos then
        board:ClearAllPoints()
        board:SetPoint(
            ResetSv.pos[1],
            ResetSv.pos[2],
            ResetSv.pos[3],
            ResetSv.pos[4],
            ResetSv.pos[5]
        )
    end

    -- 自定义
    local bg_board = CreateFrame("Frame", nil, UIParent,"BackdropTemplate");
    bg_board.Bg = board:CreateTexture(nil, "BACKGROUND")
    bg_board.Bg:SetTexture("Interface\\LevelUp\\MinorTalents")
    bg_board.Bg:SetPoint("TOP", board, "TOPLEFT", 40, 0)
    bg_board.Bg:SetSize(120, 40)
    bg_board.Bg:SetTexCoord(0, 300/320, 341/512, 407/512)
    bg_board.Bg:SetVertexColor(1, 1, 1, 0.4)
    bg_board:Show()


end

-- 拖动
function DragUi()
    board:SetScript("OnMouseDown", function(self, button)
        if button == "LeftButton" then
            self:StartMoving();
        else
            --ALADROP(board, "BOTTOMLEFT", drop_menu_table);
        end
        if button == "RightButton" then
            -- 右键双击事件处理逻辑
            print('右键鼠标双击')
            setFrame:Show()
        end
    end)

    -- 拖动事件
    board:SetScript("OnMouseUp", function(self, button)
        self:StopMovingOrSizing()
        ResetSv.pos = { self:GetPoint(), }
        for i, v in ipairs(ResetSv.pos) do
            if type(v) == 'table' then
                ResetSv.pos[i] = v:GetName()
            end
        end
    end)



end

-- 创建 line
function lineUi(tile, val, i)
    local left = board:CreateFontString(nil, "OVERLAY")
    left:SetFont(GameFontNormal:GetFont(), 16)

    left:SetPoint("TOPLEFT", board, "TOPLEFT", 2, -10 )
    -- left:SetPoint("TOPLEFT", board, "TOPLEFT", 2, - 16 * (i - 1))
    left:Show()
    left:SetText("\124cffffff00 "..tile.." \124r: ")
    local line = board:CreateFontString(nil, "OVERLAY")
    line:SetFont(GameFontNormal:GetFont(), 16)
    line:SetPoint("LEFT", left, "RIGHT", 0, 0)
    line:Show()
    line:SetText("\124cff00ff00"..val.." \124r")
    return line
end

-- init 设置界面
function initSetUi()
    local AceGUI = LibStub("AceGUI-3.0")
    setFrame = AceGUI:Create("Frame")
    setFrame:SetTitle("设置")
    setFrame:SetStatusText("作者: Sloan")
    setFrame:SetWidth(240)
    setFrame:SetHeight(180)
    setFrame:SetLayout("Flow")
    setFrame:Hide()

    local label = AceGUI:Create("Label")
    label:SetText("重置副本次数")
    label:SetFullWidth(true)
    -- label:SetJustifyH("CENTER") -- 设置水平居中对齐
    setFrame:AddChild(label)
    
    -- 重置按钮
    local resetBtn1 = AceGUI:Create("Button")
    resetBtn1:SetText("重置")
    resetBtn1:SetCallback("OnClick", function()
        -- label:SetText("内容已重置1")
        -- 保存ui
        ResetSv.num = 0
        lineUi("次数", ResetSv.num, 1)
    end)
    setFrame:AddChild(resetBtn1)

        
    -- 创建一个标签
    local label = AceGUI:Create("Label")
    label:SetText("发送私聊")
    setFrame:AddChild(label)

    -- 创建一个复选框
    local checkbox = AceGUI:Create("CheckBox")
    checkbox:SetLabel("启用")
    checkbox:SetCallback("OnValueChanged", function(widget, event, value)
        if value then
            -- print("复选框被选中")
            ResetSv.whisper = true
        else
            -- print("复选框未被选中")
            ResetSv.whisper = false
        end
    end)
    setFrame:AddChild(checkbox)

    -- 判断是否开启
    if ResetSv.whisper then
        checkbox:SetValue(true)
    else
        checkbox:SetValue(false)
    end
    



    -- 监控窗口释放
    setFrame:SetCallback("OnRelease", function(widget)
        AceGUI:Release(widget)
    end)


    -- 窗体鼠标点击
    -- board:SetScript("OnMouseDown", function(self, button)

    --     -- if button == "MiddleButton" then
    --     --     frame:Show()
    --     -- end

    --     if button == "RightButton" then
    --         -- 右键双击事件处理逻辑
    --         print('右键鼠标双击')
    --         frame:Show()
    --     end
    -- end)

end


